// haojk/pages/commissionWithdraw/index.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        radioArray: [
            { canChoice:true, membername: '普通' },
            { canChoice: false,  membername: '白银' },
            { canChoice: false,  membername: '黄金' },
            { canChoice: false,  membername: '铂金' },
            { canChoice: false,  membername: '钻石' },
        ]

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var $that = this;
        //获取缓存所欠的积分
        var lackUpNextCredit=wx.getStorageSync("lackUpNextCredit");
        var newMemberlistinfo = wx.getStorageSync("newMemberlistinfo");
        $that.setData({ newMemberlistinfo: newMemberlistinfo});

        $that.setData({
          lackUpNextCredit: lackUpNextCredit
        });
        var credit1 = wx.getStorageSync("credit1");
        $that.setData({
          credit1: credit1
        })

        var currentlevel=wx.getStorageSync('level');
        $that.setData({
            level: currentlevel
        })
        wx.getStorage({
            key: 'newLevel',
            success: function (res) {
             
                $that.setData({
                    newLevel: res.data
                })
              
            }
        })
       
        wx.getStorage({
            key: 'globalset',
            success: function (res) {
             
                $that.setData({
                    credit1_to_credit2: res.data.credit1_to_credit2

                })
                if ($that.data.lackUpNextCredit) {
                    $that.setData({
                        cashNum: ($that.data.lackUpNextCredit / $that.data.credit1_to_credit2).toFixed(2)

                    })
                }
            }
        })
         var newMemberlistinfo = $that.data.newMemberlistinfo;
       
         for (var i = 0; i < newMemberlistinfo.length;i++){
             if (newMemberlistinfo[i].name==0){
                 newMemberlistinfo[i].levelName ='普通' ;
             } else if (newMemberlistinfo[i].name == 1){
                 newMemberlistinfo[i].levelName = '白银';
             } else if (newMemberlistinfo[i].name == 2) {
                 newMemberlistinfo[i].levelName = '黄金';
             } else if (newMemberlistinfo[i].name == 3) {
                 newMemberlistinfo[i].levelName = '铂金';
             } else if (newMemberlistinfo[i].name == 4) {
                 newMemberlistinfo[i].levelName = '钻石';
             }
              
              
              if (parseInt(newMemberlistinfo[i].name) <= parseInt($that.data.level)){
                  newMemberlistinfo[i].isDisabled = true;
              }else{
                  newMemberlistinfo[i].isDisabled = false; 
              }
         }
         $that.setData({ newMemberlistinfo: newMemberlistinfo});
         
    },


   
    startUpgrade: function () {
        var $that = this;
       
        if ($that.data.cashNum == "" || $that.data.cashNum == 0 || $that.data.cashNum==undefined){
          wx.showToast({
            title: '当前等级无须升降',
            icon: 'error',
            duration: 2000
          });
          return;
        }
        wx.showModal({
            title: '确认支付',
            content: '确认支付提升等级吗？',
            success: function (res) {


               
                if (res.confirm) {
                   
                    app.util.request({
                        'url': 'entry/wxapp/LeveUpPay',
                        data: {
                            "m": 'nets_haojk',
                            "fee": $that.data.cashNum,
                            "level": $that.data.newLevel,

                        },
                        success: function (res) {
                            var newTid = "";
                        
                            if (res.data.data.tid != null && res.data.data.tid != undefined && res.data.data.tid != "") {
                                newTid = res.data.data.tid;
                            }
                            if (res.data && res.data.data) {

                               
                                wx.requestPayment({
                                    'timeStamp': res.data.data.timeStamp,
                                    'appId': res.data.data.appId,
                                    'nonceStr': res.data.data.nonceStr,
                                    'package': res.data.data.package,
                                    'signType': 'MD5',
                                    'paySign': res.data.data.paySign,
                                    'success': function (res) {
                                      
                                        app.util.request({
                                            'url': 'entry/wxapp/LeveUpResult',
                                            data: {
                                                "m": 'nets_haojk',
                                                "orderno": newTid,
                                                "level": $that.data.newLevel,

                                            },
                                            success: function (res) {
                                                //成功后回到我的页面
                                            
                                                wx.switchTab({
                                                    url: "../my/index",
                                                    success: function (e) {
                                                        var page = getCurrentPages().pop();
                                                        if (page == undefined || page == null) return;
                                                        page.onLoad();
                                                    }
                                                })
                                            }
                                        })

                                    },
                                    'fail': function (res) {
                                     
                                        //支付失败后，
                                    }
                                });
                            }
                        }
                    })
                }
            }
        })
    }
    , radioChange:function(e){
        var $that = this;
           $that.data. radioArray[0].canChoice == true
           $that.setData({
               radioArray: $that.data.radioArray
           })
          
           var newMemberlistinfo = $that.data.newMemberlistinfo; 
           //判断单选框点击时所缺少的积分
           for (var i = 0; i < newMemberlistinfo.length;i++){   
               if (e.detail.value - $that.data.level == 1 && newMemberlistinfo[i].name == $that.data.level){
                   if (newMemberlistinfo[i+1]){
                       $that.setData(
                           {
                               newLevel: e.detail.value,
                               lackUpNextCredit: newMemberlistinfo[i + 1].recharge_get - newMemberlistinfo[i].recharge_get - $that.data.credit1
                           }
                       ) 
                   }
                    
               } else if (e.detail.value - $that.data.level == 2 && newMemberlistinfo[i].name == $that.data.level){
                   if (newMemberlistinfo[i + 2]){
                       $that.setData(
                           {
                               newLevel: e.detail.value,
                               lackUpNextCredit: newMemberlistinfo[i + 2].recharge_get - newMemberlistinfo[i].recharge_get- $that.data.credit1
                           }
                       )
                   }
                    
               } else if (e.detail.value - $that.data.level == 3 && newMemberlistinfo[i].name == $that.data.level) {
                   if (newMemberlistinfo[i + 3]) {
                    
                       $that.setData(
                           {
                               newLevel: e.detail.value,
                               lackUpNextCredit: newMemberlistinfo[i + 3].recharge_get- newMemberlistinfo[i].recharge_get  - $that.data.credit1
                           }
                       )
                   }

               } else if (e.detail.value - $that.data.level == 4 && newMemberlistinfo[i].name == $that.data.level) {
                   if (newMemberlistinfo[i + 4]) {
                       $that.setData(
                           {
                               newLevel: e.detail.value,
                               lackUpNextCredit: newMemberlistinfo[i + 4].recharge_get -  newMemberlistinfo[i].recharge_get - $that.data.credit1
                           }
                       )
                   }

               }
               
               
           }
           
        //重新刷新金额
        if ($that.data.lackUpNextCredit) {
            $that.setData({
                cashNum: ($that.data.lackUpNextCredit / $that.data.credit1_to_credit2).toFixed(2)

            })
        }
    
    }

})